package cn.jc.springbootwebsocketdemo.config;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Configuration;

import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;

import org.springframework.web.socket.config.annotation.*;


@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Autowired
    GetHeaderParamInterceptor getHeaderParamInterceptor;

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").setAllowedOrigins("*")

                .withSockJS();
        registry.addEndpoint("/alone").setAllowedOrigins("*")
//                .setHandshakeHandler(new CustomHandshakeHandler())
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //  registry.setApplicationDestinationPrefixes("/app");

        // Enables a simple in-memory broker
        //   registry.enableSimpleBroker("/topic","/user");

        registry.setUserDestinationPrefix("/user");

        // 使用RabbitMQ做为消息代理，替换默认的Simple Broker
        //定给服务端发消息的地址前缀,@SendTo(XXX) 也可以重定向
        registry.setApplicationDestinationPrefixes("/app");
        // "STOMP broker rel义了服务端接收地址的前缀，也即客户端ay"处理所有消息将消息发送到外部的消息代理
        registry.enableStompBrokerRelay("/exchange", "/topic", "/queue", "/amq/queue")
                .setVirtualHost("JCChost")
                .setRelayHost("localhost")
                .setClientLogin("root")
                .setClientPasscode("root")
                .setSystemLogin("root")
                .setSystemPasscode("root")
                .setSystemHeartbeatSendInterval(5000)
                .setSystemHeartbeatReceiveInterval(4000);

    }

    /**
     * 采用自定义拦截器，获取connect时候传递的参数
     *
     * @param registration
     */
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(getHeaderParamInterceptor);
    }
}
